#include <iostream>
using namespace std;

#include "util.h"

void initAllocedMemory(void* alloced, int size)
{
	if (alloced != NULL)
	{
		memset(alloced, 0, size);
	}
}

void freeMemory(void* alloced)
{
	if (alloced != NULL)
	{
		free(alloced);
		alloced = NULL;
	}
}

size_t decodeUTF8Length(uint8_t** str)
{
	size_t len = **str;
	if ((len & 0x80) != 0) {
		(*str)++;
		len = ((len & 0x7F) << 8) | **str;
	}
	(*str)++;
	return len;
}

size_t decodeLength(uint16_t** str)
{
	size_t len = **str;
	if ((len & 0x8000) != 0) {
		(*str)++;
		len = ((len & 0x7FFF) << 16) | **str;
	}
	(*str)++;
	return len;
}

void convertToWCharString(wchar_t* dst, int dst_size, const char* src)
{
	if (dst == NULL || src == NULL || dst_size < 2 * (strlen(src) + 1))
	{
		return;
	}
	memset(dst, 0, dst_size);
	swprintf_s(dst, dst_size, L"%hs", src);
}

void convertToCharString(char* dst, int dst_size, const wchar_t* src)
{
	if (dst == NULL || src == NULL || dst_size < (wcslen(src) + 1))
	{
		return;
	}

	memset(dst, 0, dst_size);
	sprintf_s(dst, dst_size, "%ls", src);
}